<?php
/**
 * 
 * @author Joona Somerkivi <joona.somerkivi@gmail.com>
 */
class Core_View implements Core_Defines
{
    const OUTPUT_TYPE_TEMPLATE = 'Template';
    const OUTPUT_TYPE_JSON = 'Json';
    const OUTPUT_TYPE_DIRECT = 'Direct';
    
    private $data = array();
    private $baseUri = '/';
    private $content = null;
    
    private $templateDir;
    private $templateFile = null;

    private $headers = array();
    private $outputter = null;

    public function __construct()
    {
        $this->setTemplateDir(dirname(dirname(__FILE__)).'/View/Templates');
    }

    public function setBaseUri($baseUri)
    {
        $this->baseUri = $baseUri;
    }
    public function getBaseUri()
    {
        return $this->baseUri;
    }
    public function setOutputType($typeCode)
    {
        $typeCode = ucfirst(strtolower($typeCode));
        $viewClassFile = dirname(dirname(__FILE__))."/View/$typeCode.php";
        if(!file_exists($viewClassFile))
            throw new Core_View_Exception("Undefined output-type '$typeCode'");
        
        $viewClass = "View_$typeCode";
        $this->outputter = new $viewClass();
    }

    public function setTemplate($templateFile)
    {
        $this->templateFile = $templateFile;
    }
    public function getTemplate()
    {
        return $this->templateFile;
    }
    public function setTemplateDir($dir)
    {
        $this->templateDir = $dir;
    }
    public function getTemplateDir()
    {
        return $this->templateDir;
    }

    /**
     * Set a key-value pair of view
     * $key can be array holding list of key-value pairs; if so, $value will
     * be ignored
     *
     * @param string/array $key Either single key or array of key-value pairs
     * @param misc/null $value Value to be set for the key
     */
    public function set($key, $value = null)
    {
        if(is_array($key))
        {
            foreach($key as $subKey => $subValue)
                $this->set($subKey, $subValue);
        }
        else
        {
            $this->data[$key] = $value;
        }
    }

    /**
     * Remove a key-value pair from view
     * @param string $key
     */
    public function remove($key)
    {
        unset($this->data[$key]);
    }

    public function getData()
    {
        return $this->data;
    }

    public function setContent($newContent)
    {
        $this->content = $newContent;
    }

    public function getContent()
    {
        return $this->content;
    }

    public function addContent($content)
    {
        $this->content .= $content;
    }

    public function setHeader($header, $value)
    {
        $this->headers[$header] = $value;
    }

    public function resetHeaders()
    {
        $this->headers = array();
    }

    public function output()
    {
        $this->printHeaders();
        is_null($this->outputter) || $this->outputter->output($this);
    }

    public function printHeaders()
    {
        foreach($this->headers as $header => $value)
            header("$header: $value");
    }
    
    public function printOut($output, $stream = "php://output")
    {
        file_put_contents($stream, $output);
    }
}
class Core_View_Exception extends Exception { }